package com.baijiayun.liveuibase.widgets.toolbar;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;

import com.baijiayun.livecore.context.LPConstants;
import com.baijiayun.liveuibase.base.BaseUIConstant;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.widgets.popupwindow.BaseAutoArrangePopupWindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class BaseGraphMenuWindow extends BaseAutoArrangePopupWindow {

    private final int imgGroupId = 1;
    private final int widthGroupId = 2;
    private final int colorGroupId = 3;

    private final OnShapeChangeModel[] models = new OnShapeChangeModel[]{
            new OnShapeChangeModel(LPConstants.ShapeType.StraightLine
                    , R.drawable.base_ic_graph_line_unpress, R.drawable.base_ic_graph_line_press),
            new OnShapeChangeModel(LPConstants.ShapeType.Arrow
                    , R.drawable.base_ic_graph_arrow_unpress, R.drawable.base_ic_graph_arrow_press),
            new OnShapeChangeModel(LPConstants.ShapeType.DoubleArrow
                    , R.drawable.base_ic_graph_doublearrow_unpress, R.drawable.base_ic_graph_doublearrow_press),
            new OnShapeChangeModel(LPConstants.ShapeType.Rect
                    , R.drawable.base_ic_graph_rect_unpress, R.drawable.base_ic_graph_rect_press),
            new OnShapeChangeModel(LPConstants.ShapeType.Oval
                    , R.drawable.base_ic_graph_oval_unpress, R.drawable.base_ic_graph_oval_press),
            new OnShapeChangeModel(LPConstants.ShapeType.Triangle
                    , R.drawable.base_ic_graph_triangle_unpress, R.drawable.base_ic_graph_triangle_press),
            new OnShapeChangeModel(LPConstants.ShapeType.RectSolid
                    , R.drawable.base_ic_graph_solid_rect_unpress, R.drawable.base_ic_graph_solid_rect_press),
            new OnShapeChangeModel(LPConstants.ShapeType.OvalSolid
                    , R.drawable.base_ic_graph_solid_oval_unpress, R.drawable.base_ic_graph_solid_oval_press),
            new OnShapeChangeModel(LPConstants.ShapeType.TriangleSolid
                    , R.drawable.base_ic_graph_solid_triangle_unpress, R.drawable.base_ic_graph_solid_triangle_press),
    };

    public BaseGraphMenuWindow(Context context) {
        super(context);

        List<ResSet> resSets = new ArrayList<>();

        for (int i = 0; i < models.length; i++) {
            final int index = i;
            resSets.add(new ResSet(models[i].normalDrawable
                    , models[i].selectDrawable, imgGroupId, null,
                    v -> onItemClick(imgGroupId, models[index])));
        }

        resSets.add(new ResSet(getDrawable(R.drawable.base_iv_graph_group_dotted_nor)
                , getDrawable(R.drawable.base_iv_graph_group_dotted_select, true)
                , widthGroupId, null,
                v -> onItemClick(widthGroupId, paintSizeDotted)));
        resSets.add(new ResSet(getDrawable(R.drawable.base_draw_graph_width_1_unpress)
                , getDrawable(R.drawable.base_draw_graph_width_1_press, true)
                , widthGroupId, null,
                v -> onItemClick(widthGroupId, paintSizeTiny)));
        resSets.add(new ResSet(getDrawable(R.drawable.base_draw_graph_width_2_unpress)
                , getDrawable(R.drawable.base_draw_graph_width_2_press, true)
                , widthGroupId, null,
                v -> onItemClick(widthGroupId, paintSizeSmall)));
        resSets.add(new ResSet(getDrawable(R.drawable.base_draw_graph_width_3_unpress)
                , getDrawable(R.drawable.base_draw_graph_width_3_press, true)
                , widthGroupId, null,
                v -> onItemClick(widthGroupId, paintSizeMedium)));

        setResSetsMergeColors(resSets, colorGroupId, color -> onItemClick(colorGroupId, color));
        setSelectMode(GROUP_SINGLE);
        setContentPadding(0, 0, 0, 4);

        createView();

        updateCheckedIndex(0);
        updateCheckedIndex(10);

        OnShapeChangeModel initShapeModel = new OnShapeChangeModel(LPConstants.ShapeType.StraightLine
                , R.drawable.base_ic_graph_line_unpress, R.drawable.base_ic_graph_line_press);
        initShapeModel.isInitDrawable = true;
        initDefaultDrawable(initShapeModel);
    }

    private void onItemClick(int groupId, Object value) {
        if (groupId == imgGroupId) {
            onChangeShape((OnShapeChangeModel) value);
        } else if (groupId == widthGroupId) {
            onChangeWidth(new WidthSelectData(BaseUIConstant.SelectSrc.Graph, (Float) value));
        } else {
            onChangeColor(new ColorSelectData(
                    BaseUIConstant.SelectSrc.Graph, (Integer) value));
        }
    }

    /**
     * 初始化默认 图形的 Drawable
     */
    protected abstract void initDefaultDrawable(OnShapeChangeModel initShapeModel);

    /**
     * 选择图形回调
     */
    protected abstract void onChangeShape(OnShapeChangeModel onShapeChangeModel);

    // region 所有宽度
    private final float paintSizeTiny = 2f;

    private final float paintSizeSmall = 4f;

    private final float paintSizeMedium = 6f;
    // 虚线
    private final float paintSizeDotted = 0f;
    // endregion

    /**
     * 选择宽度回调, 0为虚线
     * @see #paintSizeDotted
     */
    protected abstract void onChangeWidth(WidthSelectData widthSelectData);

    /**
     * 选择颜色
     */
    protected abstract void onChangeColor(ColorSelectData colorSelectData);

    @Override
    protected void initViewDataHashMap() {
        HashMap<Integer, BaseAutoArrangePopupWindow.GroupViewData> groupViewDataHashMap = new HashMap<>();
        groupViewDataHashMap.put(imgGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 140, 38, COMMON_CUSTOM_ITEM_SIZE
                        , COMMON_CUSTOM_ITEM_SIZE, 3));
        groupViewDataHashMap.put(widthGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 130, 32, COMMON_CUSTOM_COLORS_ITEM_SIZE
                        , COMMON_CUSTOM_ITEM_SIZE, 4));
        groupViewDataHashMap.put(colorGroupId,
                new BaseAutoArrangePopupWindow.GroupViewData(context, 130, 32, COMMON_CUSTOM_COLORS_ITEM_SIZE
                        , COMMON_CUSTOM_COLORS_ITEM_SIZE, 4));
        this.viewDataHashMap = groupViewDataHashMap;
    }

    @Override
    public void show(View anchor) {
        showWithViewOfDirection(anchor, LEFT_BOTTOM);
    }

    /**
     * 图形变更数据传输数据结构
     */
    public class OnShapeChangeModel {
        /**
         * 当前选中的图形
         */
        LPConstants.ShapeType shapeType;
        /**
         * 当前选中图形选中时的 drawable
         */
        Drawable selectDrawable;
        /**
         * 当前选中图形非选中时的 drawable
         */
        Drawable normalDrawable;
        /**
         * 是否为初始化默认图形的 Drawable，仅在首次加载时用于传出默认图形
         */
        boolean isInitDrawable = false;

        public OnShapeChangeModel(LPConstants.ShapeType shapeType, int normalDrawableId, int selectDrawableId) {
            this.shapeType = shapeType;
            this.selectDrawable = BaseGraphMenuWindow.this.getDrawable(selectDrawableId, true);
            this.normalDrawable = BaseGraphMenuWindow.this.getDrawable(normalDrawableId);
        }

        public LPConstants.ShapeType getShapeType() {
            return shapeType;
        }

        public Drawable getSelectDrawable() {
            return selectDrawable;
        }

        public Drawable getNormalDrawable() {
            return normalDrawable;
        }

        public boolean isInitDrawable() {
            return isInitDrawable;
        }

        public void setInitDrawable(boolean initDrawable) {
            isInitDrawable = initDrawable;
        }
    }
}
